﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using BF.Model;
using BF.IBLL.BaseSystem;
using Base.Core.UnityFactory;
using System.Web.SessionState;
using BF.Web.App_Code;

namespace BF.Web.pages.BaseSystem.RolePermissionManagement.RolesManagement
{

    public class NodeInfo
    {
        //定义jsondata类，存放节点数据
        public string id;
        public string text;
        public bool leaf;
        public string cls;
        public string icon;
        public string iconCls;
        public bool expanded;
        public bool singleClickExpand;
        public bool Checked;
        public string listeners;
        public bool disabled;
        public List<NodeInfo> children = new List<NodeInfo>();//存放子节点
    }

    public class NodeParam
    {
        public string sKey;
        public string sName;
        public int iLevel;
        public int iType;
        public int iSort;
    }


    /// <summary>
    /// LoadCheckBoxTreeHandler 的摘要说明
    /// </summary>
    public class LoadCheckBoxTreeHandler : BaseHandler
    {

        //public List<NodeInfo> jsdata = new List<NodeInfo>();

        protected override  void ActionHandler(string context)
        {
            string nodeID = _context.Request.QueryString["NodeID"];
            string roleName = _context.Request.QueryString["RoleName"];
            string isCurrentUserCreate = _context.Request.QueryString["isCurrentUserCreate"];
            //roleName = context.Server.UrlEncode(roleName);
            Isys_FunctionBLL iBLL = IOCFactory.GetIOCResolve<Isys_FunctionBLL>();
            Isys_PermissionBLL iPermissionBLL = IOCFactory.GetIOCResolve<Isys_PermissionBLL>();
            List<sys_Function> list = iPermissionBLL
                .GetFunctionListByUserID(GetSessionCurrentUserInfo.gUserID.ToString())
                .Where(P => P.bFlag == true)
                .ToList();

            List<sys_Function> roleHaveList = iPermissionBLL.GetFunctionListByRoleName(roleName);
            //iPermissionBLL.GetFunctionListByUserID()

            NodeInfo SelfNode = new NodeInfo();
            SelfNode.id = nodeID;
            LoadNodes(ref SelfNode, list, roleHaveList, isCurrentUserCreate);

            string strJson = ToJson(SelfNode.children.ToArray()).Replace("Checked", "checked");
            strJson = strJson
                .Replace("\"\\u003c\\u003c", "{")
                .Replace("\\u003e\\u003e\"", "}")
                .Replace("\\u0027", "")
                .Replace("\\\"", "'")
                ;

            _context.Response.Write(strJson);
        }

        private void LoadNodes(ref NodeInfo parentNode, List<sys_Function> list, List<sys_Function> roleHaveList, string isCurrentUserCreate)
        {
            string strID = parentNode.id;
            //List<sys_Function> currentList = list
            //    .Where(P => P.gParentKey.Equals(new Guid(strID)))
            //    .OrderBy(P => P.iSort)
            //    .ToList();

            List<sys_Function> currentList = null;
            if (strID == "Null")
                currentList = list
                    .Where(P => P.bIsRoot == true)
                    .OrderBy(P => P.iSort)
                    .ToList();
            else
                currentList = list
                    .Where(P => P.gParentKey.Equals(new Guid(strID)))
                    .OrderBy(P => P.iSort)
                    .ToList();

            parentNode.leaf = currentList.Count == 0;
            NodeInfo node = null;
            foreach (var item in currentList)
            {
                node = new NodeInfo();
                //node.Checked = false;
                node.id = item.gKey.ToString();
                node.singleClickExpand = false;
                node.text = item.sName;
                node.disabled = (isCurrentUserCreate == "true");

                NodeParam nodeParam = new NodeParam()
                {
                    sKey = item.gKey.ToString(),
                    sName = item.sName,
                    iLevel = item.iLevel,
                    iType = item.iType,
                    iSort = item.iSort ?? -1

                };
                string param = ToJson(nodeParam);
                node.listeners = "";

                if (roleHaveList
                    .Where(P => P.gKey.Equals(item.gKey))
                    .Count() > 0)
                {
                    node.Checked = true;
                }

                node.singleClickExpand = true;
                node.expanded = true;

                switch (item.iType)
                {
                    case 1:
                        node.iconCls = "icon-folder";
                        break;
                    case 2:
                        node.iconCls = "icon-file";
                        break;
                    case 3:
                        node.iconCls = "icon-button";
                        break;
                    default:
                        break;
                }
                parentNode.children.Add(node);
                LoadNodes(ref node, list, roleHaveList, isCurrentUserCreate);
            }
        }

        public string ToJson(object o)
        {
            //序列化对象为json数据，很重要！
            JavaScriptSerializer j = new JavaScriptSerializer();
            return j.Serialize(o);
        }

    }
}